//version 16.0
clear all
set more off
capture log close

/* DESCRIPTION:
	This file creates the US figures (5b and 5d) for:
		*Figure 5: Parental assortative mating in Sweden and the US
	
*/

//ssc install binscatter

*USE the ranked MAIN SAMPLE 
use *newid *LAB* AGE *cohort *LABYR* *MF* *empl* *schmax year female if year>=1985 using ${projdata}/analysis-sample-main.dta, clear

gen AGEC1=year-cohort-40 
gen f_LABAGE_1=f_LABYR-f_cohort 
gen m_LABAGE_1=m_LABYR-m_cohort 

forval i=2/4{
	gen AGEC`i'=AGEC1^`i'
	gen f_LABAGE_`i'=f_LABAGE_1^`i'
	gen m_LABAGE_`i'=m_LABAGE_1^`i'
}
*Use decomposition sample with mothers AND fathers
 keep if pm_LAB!=. & m_LABAGE_1!=. & pf_LAB!=. & f_LABAGE_1!=.
 keep if m_emplavg!=. & f_emplavg!=.
 keep if m_schmax!=. & f_schmax!=.
*Residualize all measures using mother's AND father's age (quartic)
 foreach var in pLAB employ schmax pm_LAB m_emplavg m_schmax pf_LAB f_emplavg f_schmax  {
	qui regress `var' m_LABAGE_? f_LABAGE_?   AGEC? i.year
	predict `var'_r, resid
 }
*Need yearlist for loops due to biennial surveys
 levelsof year, local(yearlist) 
 rename AGE AGEC
*Drop middle period
 drop if year>1995 & year<2007
*Dummy for late period
 gen t=(year>=2007)
*Rescale residualized measures
  replace pLAB_r = pLAB_r+50
  replace pm_LAB_r = pm_LAB_r+50
  replace pf_LAB_r = pf_LAB_r+50
  sum m_schmax
  replace m_schmax_r = m_schmax_r+r(mean)
  sum f_schmax
  replace f_schmax_r = f_schmax_r+r(mean)
*Figure 5b
binscatter  m_schmax_r f_schmax_r, by(t)	ytitle("Mean schooling (mother)") xtitle("Schooling (father)") n(10) ///
  legend(order(1 2) label(1 "1985-1995") label(2 "2008-2018")) msymbol(O Oh)
  graph export "${tabfig}/fig5b.pdf", replace
  graph export "${tabfig}/fig5b.eps", replace
/*Figure 5d
binscatter pm_LAB_r pf_LAB_r, by(t)	ytitle("Mean income rank (mother)") xtitle("Father's income rank") n(10) ///
  legend(order(1 2) label(1 "1985-1995") label(2 "2008-2018")) 
  graph export "${tabfig}/fig5d_lfit.pdf", replace
  graph export "${tabfig}/fig5d_lfit.eps", replace
*/
*Figure 5d
binscatter pm_LAB_r pf_LAB_r, by(t)	ytitle("Mean income rank (mother)") xtitle("Father's income rank") n(10) linetype(qfit) ///
  legend(order(1 2) label(1 "1985-1995") label(2 "2008-2018")) msymbol(O Oh)
  graph export "${tabfig}/fig5d.pdf", replace
  graph export "${tabfig}/fig5d.eps", replace
